//实现语音识别子服务
#include <brpc/server.h>
#include <butil/logging.h>

#include "asr.hpp"      // 语音识别模块封装
#include "etcd.hpp"     // 服务注册模块封装
#include "Logger.hpp"   // 日志模块封装
#include "speech.pb.h"  // protobuf框架代码
namespace chatroom{
    // 1  brpc服务端
    class SpeechServiceImp : public chatroom::SpeechService{
    public:
        SpeechServiceImp(chatroom::ASRClient::ptr& client):_client(client){}
        ~SpeechServiceImp(){}
        void SpeechRecognition(google::protobuf::RpcController* controller,
            const ::chatroom::SpeechRecognitionReq* request,
            ::chatroom::SpeechRecognitionRsp* response,
            ::google::protobuf::Closure* done){
                brpc::ClosureGuard guard(done);
                std::string err;
                std::string res = _client->recogniztion(request->speech_content(),err);
                if(res.empty())
                {
                    Logger_ERROR("{}语音识别失败",request->request_id());
                    response->set_request_id(request->request_id());
                    response->set_success(false);
                    response->set_errmsg(err);
                    return;
                }
                response->set_request_id(request->request_id());
                response->set_success(true);
                response->set_recognition_result(res);
            }
    private:
        chatroom::ASRClient::ptr _client;
    };
    class speechServer{
        public:
            using ptr = std::shared_ptr<speechServer>;
            speechServer(chatroom::ASRClient::ptr& client,std::shared_ptr<brpc::Server>& server,chatroom::Registery::ptr& rclient)
            :_client(client),_server(server),_rclient(rclient){}
            ~speechServer(){}
            void start(){
                _server->RunUntilAskedToQuit();
            }
        private:
            chatroom::ASRClient::ptr _client;
            std::shared_ptr<brpc::Server> _server;
            chatroom::Registery::ptr _rclient;

    };
    class speechServerBuild{
        public:
            void make_asr_object(std::string& app_id,std::string& api_key,std::string& secret_key)
            {
                _client = std::make_shared<chatroom::ASRClient>(app_id,api_key,secret_key);
            }
            void make_rpc_server(int32_t port,int32_t timeout,int32_t num_threads)
            {
                _server = std::make_shared<brpc::Server>();
                //3 往服务器中添加服务
                SpeechServiceImp* speech = new SpeechServiceImp(_client);
                int ret = _server->AddService(speech,brpc::ServiceOwnership::SERVER_OWNS_SERVICE);
                if(ret == -1)
                {
                    Logger_ERROR("添加服务失败");
                    abort();
                }
                //4 启动服务器
                brpc::ServerOptions options;
                options.idle_timeout_sec = timeout; //指定超时连接时间，-1表示超时一直在连接着
                options.num_threads = num_threads;//cpu线程数量
                ret = _server->Start(port,&options);
                if(ret == -1)
                {
                    Logger_ERROR("启动服务器失败");
                    abort();
                }
            }
            void  make_reg_object(std::string& register_host,const std::string& hostname,std::string& access_host)
            {
                _rclient = std::make_shared<chatroom::Registery>(register_host);
                _rclient->Register(hostname,access_host);
            }
            speechServer::ptr build()
            {
                if(!_client){
                    Logger_ERROR("语音子模块没有初始化");
                    abort();
                }
                if(!_server){
                    Logger_ERROR("RPC服务子模块没有初始化");
                    abort();
                }
                if(!_rclient){
                    Logger_ERROR("注册服务没有初始化");
                    abort();
                }
                return std::make_shared<speechServer>(_client,_server,_rclient);
            }
        private:
            chatroom::ASRClient::ptr _client;
            std::shared_ptr<brpc::Server> _server;
            chatroom::Registery::ptr _rclient;
    };
}